/**
 * 
 */
package com.mustafaiev.tair.cts.facade;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dozer.Mapper;

import com.mustafaiev.tair.cts.dao.IPurchaseDAO;
import com.mustafaiev.tair.cts.exeptions.DataNotRemovedException;
import com.mustafaiev.tair.cts.exeptions.DataNotStoredException;
import com.mustafaiev.tair.cts.model.Purchase;
import com.mustafaiev.tair.cts.tapestry.dto.PurchaseDTO;

/**
 * @author tair
 *
 */
public class PurchaseFacade implements IPurchaseFacade {

    private final Mapper mapper;

    private final IPurchaseDAO purchaseDao;

    private static final Logger LOGGER = Logger
            .getLogger(PurchaseFacade.class);

    public PurchaseFacade(final IPurchaseDAO purchaseDao, final Mapper mapper) {
        this.purchaseDao = purchaseDao;
        this.mapper = mapper;
    }

    public void save(final PurchaseDTO dto) {
        final Purchase purchase = mapper.map(dto, Purchase.class);
        try {
            purchaseDao.doSave(purchase);
        }
        catch (final DataNotStoredException e) {
            LOGGER.error(e);
        }
    }

    public void saveBatch(final List<PurchaseDTO> purchases) {
        final List<Purchase> list = new ArrayList<Purchase>();
        for (final PurchaseDTO purchaseDTO : purchases) {
            final Purchase purchase = mapper.map(purchaseDTO, Purchase.class);
            list.add(purchase);
        }
        try {
            purchaseDao.saveBatch(list);
        }
        catch (final DataNotStoredException e) {
            LOGGER.error(e);
        }
    }

    public void delete(final PurchaseDTO purchaseDTO) {
        try {
            purchaseDao.doDeleteById(purchaseDTO.getId());
        }
        catch (final DataNotRemovedException e) {
            LOGGER.error(e);
        }
    }

    public void deleteBatch(final List<PurchaseDTO> purchases) {
        final List<Purchase> list = new ArrayList<Purchase>();
        for (final PurchaseDTO purchaseDTO : purchases) {
            final Purchase purchase = mapper.map(purchaseDTO, Purchase.class);
            list.add(purchase);
        }
        try {
            purchaseDao.deleteBatch(list);
        }
        catch (final DataNotRemovedException e) {
            LOGGER.error(e);
        }
    }

    public List<PurchaseDTO> retrievePurchases() {
        final List<Purchase> purchases = purchaseDao.retrievePurchases();
        final List<PurchaseDTO> purchaseDtos = new ArrayList<PurchaseDTO>();

        for (final Purchase purchase : purchases) {
            final PurchaseDTO purchaseDTO = mapper.map(purchase, PurchaseDTO.class);
            purchaseDtos.add(purchaseDTO);
        }
        return purchaseDtos;
    }

    public PurchaseDTO retrievePurchase(final Long id) {
        final Purchase purchase = purchaseDao.retrievePurchase(id);
        final PurchaseDTO dto = mapper.map(purchase, PurchaseDTO.class);
        return dto;
    }

    public List<PurchaseDTO> retrievePurchasesForBasket(final Long basketId) {
        final List<Purchase> purchases = purchaseDao.retrievePurchasesForBasket(basketId);
        final List<PurchaseDTO> purchaseDtos = new ArrayList<PurchaseDTO>();
        for (final Purchase purchase : purchases) {
            final PurchaseDTO purchaseDTO = mapper.map(purchase, PurchaseDTO.class);
            purchaseDtos.add(purchaseDTO);
        }
        return purchaseDtos;
    }



}
